﻿using System;
using System.Globalization;
using FWPDocumentation.Common.DbTools;

namespace FWPDocumentation.Entities
{
    public class Student
    {
        

        private long Id { get; set; }

        public string Surname { get; set; }

        public string Name { get; set; }

        public string MiddleName { get; set; }

        public Group Group { get; set; }

        public long PIN { get; set; }

        public double GPA { get; set; }

        public int Test { get; set; }

        public bool MedicalCommission { get; set; }

        public bool ProfSelection { get; set; }

        public bool PhysSelection { get; set; }

        public bool Payment { get; set; }

        public Student()
        {
            
        }
        public Student(long id,string surname,string name,string middleName,Group group,long pin, double gpa, 
            int test, bool medCommision,bool profSelection,  bool physSelection,bool payment)
        {
            this.Id = id;
            this.Surname = surname;
            this.Name = name;
            this.MiddleName = middleName;
            this.Group = group;
            this.PIN = pin;
            this.GPA = gpa;
            this.Test = test;
            this.MedicalCommission = medCommision;
            this.ProfSelection = profSelection;
            this.PhysSelection = physSelection;
            this.Payment = payment;
        }

        public static Student CreateStudent( string surname,string name,string niddleName,Group group,long pin, double gpa, 
            int test, bool medCommision, bool profSelection, bool physSelection,bool payment)
        {
            var insertCommand =
                new SqlCommand("INSERT INTO maininfo (Surname, Name, MiddleName,ID, Group_idGroup) VALUES ('" +
                               surname + "','" + name + "','" + niddleName +"','"+pin+ "','" + group.idGroup + "');" + '\n' +
                               "INSERT INTO admission (MainInfo_IdMainInfo, GPA, Test, MedicalCommission, ProfSelection, PhysSelection, Payment, AdmissionYear) " +
                               "VALUES (LAST_INSERT_ID(),'" + gpa.ToString("0.00", CultureInfo.InvariantCulture) + "','" + test + "','" + Convert.ToInt16(medCommision) + "','" +
                               Convert.ToInt16(profSelection) + "','" +Convert.ToInt16(physSelection) + "','" + Convert.ToInt16(payment) + "',YEAR(CURDATE()));");
            insertCommand.ExecuteNonQuery();
            insertCommand.CommandText = "SELECT LAST_INSERT_ID() FROM maininfo";
            long iddd = insertCommand.ExecuteScalar<long>();
            
            return new Student(iddd, surname, name, niddleName, group, pin, gpa, test, medCommision, profSelection,
                               physSelection, payment);
        }


    }
}
